# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=evince
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=48.0
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Document (PostScript, PDF) viewer (mingw-w64)"
url="https://wiki.gnome.org/Apps/Evince"
msys2_repository_url="https://gitlab.gnome.org/GNOME/evince"
msys2_references=(
  "cpe: cpe:/a:gnome:evince"
)
license=("spdx:GPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas"
         "${MINGW_PACKAGE_PREFIX}-appstream-glib"
         "${MINGW_PACKAGE_PREFIX}-gspell"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libgxps"
         "${MINGW_PACKAGE_PREFIX}-libspectre"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-libhandy"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-desktop-file-utils"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-yelp-tools")
source=(https://download.gnome.org/sources/${_realname}/${pkgver%%.*}/${_realname}-${pkgver}.tar.xz
        0003-mingw-dont-have-fcntl.patch
        0008-disable-pdf-document-load-fd.patch
        0009-evince-pe-icon.patch::https://gitlab.gnome.org/GNOME/evince/-/merge_requests/701.diff
        evince.ico::https://gitlab.gnome.org/GNOME/evince/-/raw/24967ecf7f658bb69e512d6e09287c1ccc02c11c/shell/evince.ico?inline=false
        0010-no-console.patch)
sha256sums=('cd2f658355fa9075fdf9e5b44aa0af3a7e0928c55614eb1042b36176cf451126'
            '5c011fb1dec0564dcea06d145a2cc3572641dcd021477eb1172ecddd9cacbffd'
            'f9e2010366f9185e1f6a18b9ac7207c792d970fd1636b3f3bd329f58259cf89e'
            '6b5a22b5b7ad8bd964c579ccd31a4776eecf0d33b3f10ffacb64774dfb70ff10'
            '0b7493eef41f0907cb523d9165c7a67a13f05c7ee9fb5cce42f7ff3342bc4e4e'
            'db40297acd4a5eaf7cc94464737020a22c2f9a952683f55adfc24a392de1af24')
noextract=(${_realname}-${pkgver}.tar.xz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xJf "${srcdir}"/${_realname}-${pkgver}.tar.xz || true
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    0003-mingw-dont-have-fcntl.patch \
    0008-disable-pdf-document-load-fd.patch \
    0009-evince-pe-icon.patch \
    0010-no-console.patch

  cp "${srcdir}/evince.ico" "shell/evince.ico"
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  CFLAGS+=" -Wno-implicit-function-declaration" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
   ${MINGW_PREFIX}/bin/meson setup \
     --prefix=${MINGW_PREFIX} \
     --wrap-mode=nodownload \
     --auto-features=enabled \
     --buildtype=plain \
     -Dplatform=win32 \
     -Ddbus=false \
     -Dnautilus=false \
     -Dthumbnail_cache=disabled \
     -Ddvi=disabled \
     -Dgtk_doc=false \
     -Dgtk_unix_print=disabled \
     "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" meson install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
